<?php

namespace Handler;

use Model\GameData\RepClientData;
use GatewayWorker\Lib\Gateway;
use Config\Cfg;
use Model\User;

class PushHandler {
	/**
	 * 单例
	 *
	 * @var PushHandler
	 */
	private static $m_Instance;
	/**
	 * 构造函数
	 */
	private function __construct() {
	}
	/**
	 * 复制方法
	 */
	private function __clone() {
	}
	/**
	 * 获取单例
	 *
	 * @return PushHandler
	 */
	public static function mb_GetInstance() {
		if (! (self::$m_Instance instanceof self)) {
			self::$m_Instance = new self ();
		}
		return self::$m_Instance;
	}
	/**
	 *
	 * @param User $user        	
	 */
	public function mb_UserForcedOffline($user) {
		$this->mb_OutTextMessage ( $user->m_ClientId, 'UserForcedOffice', ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 *
	 * @param User $user        	
	 */
	public function mb_UserDataPush($user) {
		$this->mb_OutTextMessage ( $user->m_ClientId, 'UserDataPush', ErrCode_Normal, json_encode ( $user->mb_GetData () ) );
	}
	/**
	 *
	 * @param User $user        	
	 */
	public function mb_UserInfoPush($user) {
		$this->mb_OutTextMessage ( $user->m_ClientId, 'UserInfoPush', ErrCode_Normal, json_encode ( $user->mb_GetInfo () ) );
	}
	/**
	 *
	 * @param User $user        	
	 */
	public function mb_UserAllDataPush($user) {
		$this->mb_OutTextMessage ( $user->m_ClientId, 'UserAllDataPush', ErrCode_Normal, json_encode ( $user->mb_GetAllData () ) );
	}
	/**
	 * 服务器进入维护
	 */
	public function mb_EnterMaintenancePush() {
		$users = UserHandler::mb_GetInstance ()->mb_GetAllUsers ();
		if (count ( $users ) == 0)
			return;
		foreach ( $users as $user ) {
			$this->mb_OutTextMessage ( $user->m_ClientId, 'EnterMaintenancePush', ErrCode_Normal, ErrMsg_Normal );
		}
	}
	/**
	 * 推送默认滚动公告更新
	 *
	 * @param unknown $msg        	
	 */
	public function mb_DefaultBroadcastPush($msg) {
		$users = UserHandler::mb_GetInstance ()->mb_GetAllUsers ();
		if (count ( $users ) == 0)
			return;
		foreach ( $users as $user ) {
			$this->mb_OutTextMessage ( $user->m_ClientId, 'DefaultBroadcastPush', ErrCode_Normal, $msg );
		}
	}
	/**
	 * 推送更新日志信息
	 *
	 * @param User $user        	
	 * @param unknown $type        	
	 */
	public function mb_UpdateDataRecordPush($user, $type) {
		$this->mb_OutTextMessage ( $user->m_ClientId, 'UpdateDataRecordPush', ErrCode_Normal, $type );
	}
	/**
	 * 推送更新大厅信息
	 */
	public function mb_UpdateLobbyInfoPush() {
		$users = UserHandler::mb_GetInstance ()->mb_GetAllUsers ();
		if (count ( $users ) == 0)
			return;
		foreach ( $users as $user ) {
			$this->mb_OutTextMessage ( $user->m_ClientId, 'UpdateLobbyInfoPush', ErrCode_Normal, json_encode ( LobbyHandler::mb_GetInstance ()->mb_GetLobbyInfo ( 1 ) ) );
		}
	}
	/**
	 * 推送更新游戏信息
	 */
	public function mb_UpdateAppInfoPush() {
		$users = UserHandler::mb_GetInstance ()->mb_GetAllUsers ();
		if (count ( $users ) == 0)
			return;
		foreach ( $users as $user ) {
			$this->mb_OutTextMessage ( $user->m_ClientId, 'UpdateAppInfo', ErrCode_Normal, json_encode ( LobbyHandler::mb_GetInstance ()->mb_GetAppInfo () ) );
		}
	}
	/**
	 * 发送消息
	 *
	 * @param string $clientId        	
	 * @param string $cmd        	
	 * @param string $result        	
	 * @param string $data        	
	 */
	protected function mb_OutTextMessage($clientId, $recNo, $result, $data) {
		$repData = new RepClientData ();
		$repData->m_RecNo = $recNo;
		$repData->m_Result = $result;
		$repData->m_Data = $data;
		$repData->m_Sign = md5 ( $recNo . $result . $data . Cfg::MSG_MD5 );
		Gateway::sendToClient ( $clientId, json_encode ( $repData ) );
	}
}